package com.xianbeiying.app.ckgl.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Environment;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * 
 * @Title: Tools.java
 * @Package com.ahhf.singjto.tools
 * @author Zwj
 * 网络的判断和时间获取
 */

public class Tools {
	public static boolean isDebug = true;
	public static String msg = "网络没有连接哦，快去设置吧。";
	@SuppressLint("SimpleDateFormat")
	private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static DateFormat df1;
	private static String date1;
	private static long diff;
	// 地址
	public static String Laction = null;
	// 改变的地址
	public static String ChangeLaction = null;
	// 天，时，分，秒
	static long days = 0;
	static long hours = 0;
	static long minutes = 0;
	static long ss = 0;

	// 判断sd卡有么有
	public static boolean ExistSDCard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			/* 得到SD卡得路径 */
		Environment.getExternalStorageDirectory();
			return true;
		} else {
			return false;
		}
	}

	// 截取字符串
	public static String getDataFormat(String str) {
		String result = "";
		String names[] = str.split(" ");
		result = names[0];
		return result;
	}

	/**
	 * @Name: isNetwork
	 * @Description:判断网络
	 */
	public static boolean isNetwork(Context context) {
		try {
			ConnectivityManager connManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			// 获取代表联网状态的NetWorkInfo对象

			NetworkInfo info = connManager.getActiveNetworkInfo();
			if (info != null && info.isAvailable()) {
				// 当前的网络连接可用
				State mState = connManager.getNetworkInfo(
						ConnectivityManager.TYPE_MOBILE).getState();
				// GPRS网络
				State wState = connManager.getNetworkInfo(
						ConnectivityManager.TYPE_WIFI).getState();
				// WIFI网络
				if (State.CONNECTED == mState) {
					// GPRS网络已连接
					return true;
				} else if (State.CONNECTED == wState) {
					// WIFI网络已连接
					return true;
				}
			} else {
				return false;
				// 当前网络不可用
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// 获取当前是什么时候
	@SuppressLint("SimpleDateFormat")
	public static String timeContent() {
		long currentTimestamp = System.currentTimeMillis();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String timeStamp = sdf.format(new Date(currentTimestamp));
		String[] dds = new String[] {};
		// 分取系统时间 小时分
		dds = timeStamp.split(":");
		int dhs = Integer.parseInt(dds[0]);
		if (06 <= dhs && dhs <= 11) {
			return "早上好";

		} else if (12 <= dhs && dhs <= 17) {
			return "下午好";

		} else if (18 <= dhs && dhs <= 23) {
			return "晚上好";
		} else if (00 <= dhs && dhs < 05) {
			return "晚上好";
		}
		return null;
	}

	// 用于时间的获取和计算;
	@SuppressLint("SimpleDateFormat")
	public static long getSs(String mytimes, String myTimes, int i) {
		try {
			df1 = new SimpleDateFormat(paserTime(Integer.valueOf(mytimes)));
			date1 = df1.format(new Date());
			Date d1 = df.parse(date1);
			Date d2 = df.parse(myTimes);
			diff = (d1.getTime() - d2.getTime());
		} catch (ParseException e) {
			//TODO Auto-generated catch block
			e.printStackTrace();
		}
		days = diff / (1000 * 60 * 60 * 24);
		hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		minutes = (diff - days * (1000 * 60 * 60 * 24) - hours
				* (1000 * 60 * 60))
				/ (1000 * 60);
		ss = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes
				* (1000 * 60)) / 1000;
		if (i == 1) {
			return hours;
		} else if (i == 2) {
			return minutes;
		} else if (i == 3) {
			return ss;
		}
		return days;
	}

	// 通过访问获取网络时间
	public static long getNetTime() {
		URL url = null;
		try {
			url = new URL("http://www.365hgo.com");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		URLConnection uc = null;
		try {
			uc = url.openConnection();
			uc.connect();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return uc.getDate();
	}

	// 解析时间
	public static String paserTime(int time) {
		System.setProperty("user.timezone", "Asia/Shanghai");
		TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
		TimeZone.setDefault(tz);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String times = format.format(new Date(time * 1000L));
		return times;
	}

	// 获取当前时间日期
	public static String getNowTime() {
		long currentTimestamp = System.currentTimeMillis();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String timeStamp = sdf.format(new Date(currentTimestamp));
		return timeStamp;
	}

	// 获取时间差数组
	@SuppressLint("SimpleDateFormat")
	public static long[] deltaT(String frist, String end) {
		try {
			long[] dtime = new long[4];
			df1 = new SimpleDateFormat(Tools.paserTime(Integer.valueOf(end)));
			date1 = df1.format(new Date());
			Date d1 = df.parse(date1);
			Date d2 = df.parse(frist);
			long diff = (d1.getTime() - d2.getTime());
			days = diff / (1000 * 60 * 60 * 24);
			hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
			minutes = (diff - days * (1000 * 60 * 60 * 24) - hours
					* (1000 * 60 * 60))
					/ (1000 * 60);
			ss = (diff - days * (1000 * 60 * 60 * 24) - hours
					* (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
			dtime[0] = days;
			dtime[1] = hours;
			dtime[2] = minutes;
			dtime[3] = ss;
			return dtime;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
